package org.xtframe.entity;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xtframe.bean.SystemConfig;
import org.xtframe.dao.DataBaseDao;
import org.xtframe.util.CommonUtil;
import org.xtframe.util.SpringUtil;
import org.xtframe.util.SystemConfigUtil;

public class Role {
	
	private final Log logger = LogFactory.getLog(getClass());
	
	private DataBaseDao dataBaseDao;
	private SystemConfig systemConfig;
	
	//角色ID
	private String roleId;
	//角色名称
	private String roleName;
	//角色顺序号
	private int orderNo;
	//角色对应的所有菜单
	private ArrayList <Integer>menu;
	//角色对应的所有sql
	private ArrayList <String>sql;
	//角色对应的所有逻辑
	private ArrayList <String>logic;
	//角色对应的所有资源
	private ArrayList <String>res;
	
	public Role(){
		dataBaseDao = (DataBaseDao) SpringUtil.getBean("dataBaseDao");
		systemConfig = SystemConfigUtil.getSystemConfig();
		
		menu = new ArrayList <Integer>();
		sql = new ArrayList <String>();
		logic = new ArrayList <String>();
		res = new ArrayList <String>();
	}
	
	public boolean init(){
		try {
			if(roleId == null) return false;
			
			menu.clear();
			String strSql = CommonUtil.createSql(systemConfig.getRoleInitMenuSql(), roleId);
			List<String> result = dataBaseDao.executeQuery(strSql, 1, null);
			if(result == null) return false;
			for(String str : result){
				menu.add(Integer.valueOf(str));
			}
			
			sql.clear();
			strSql = CommonUtil.createSql(systemConfig.getRoleInitSqlSql(), roleId);
			result = dataBaseDao.executeQuery(strSql, 1, null);
			if(result == null) return false;
			for(String str : result){
				sql.add(str.trim());
			}
			
			logic.clear();
			strSql = CommonUtil.createSql(systemConfig.getRoleInitLogicSql(), roleId);
			result = dataBaseDao.executeQuery(strSql, 1, null);
			if(result == null) return false;
			for(String str : result){
				logic.add(str.trim());
			}
			
			res.clear();
			strSql = CommonUtil.createSql(systemConfig.getRoleInitResSql(), roleId);
			result = dataBaseDao.executeQuery(strSql, 1, null);
			if(result == null) return false;
			for(String str : result){
				res.add(str.trim());
			}
			
			return true;
		} catch (NumberFormatException e) {
			logger.error(e.toString());
		}
		return false;
	}
	
	public boolean isInSQL(String sqlId){
		boolean result = false;
		for(String str : sql){
			if(str.equals(sqlId)){
				result = true;
				break;
			}
		}
		return result;
	}
	
	public boolean isInLogic(String logicId){
		boolean result = false;
		for(String str : logic){
			if(str.equals(logicId)){
				result = true;
				break;
			}
		}
		return result;
	}
	
	public boolean isInRes(String resId){
		boolean result = false;
		for(String str : res){
			if(str.equals(resId)){
				result = true;
				break;
			}
		}
		return result;
	}
	
	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public int getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(int orderNo) {
		this.orderNo = orderNo;
	}
	
	public ArrayList<String> getLogic() {
		return logic;
	}

	public ArrayList<Integer> getMenu() {
		return menu;
	}
}
